DataGridViewContextMenuSample (DataGridView加入右鍵選單)
private void DataGridView1_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e)
{
if (contextMenu == null)
{
contextMenu = new ContextMenuStrip();
toolstrip.Text = "刪除";
contextMenu.Items.Add(toolstrip);
}
e.ContextMenuStrip = contextMenu;
//dgvData.Rows[e.RowIndex].Selected = true;//選擇該列(使它整列變藍色選取樣子)
}
private void toolstrip_Click(object sender, EventArgs e)
{
if (MessageBox.Show("確定要刪除該筆資料?", "警告!!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//如果按下yes就會執行刪除指令,這邊就看個人發揮囉!!
}
}
评论